package com.auemall.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.ProductLadderPricePOMapper;
import com.auemall.dao.ProductSkuPOMapper;
import com.auemall.dao.TAttributeValuesPOMapper;
import com.auemall.dao.TCategoryBrandsPOMapper;
import com.auemall.dao.TFavoritePOMapper;
import com.auemall.dao.THomeProductPagePOMapper;
import com.auemall.dao.TProductAttributePOMapper;
import com.auemall.dao.TProductSkuItemsPOMapper;
import com.auemall.dao.TProductbrandPOMapper;
import com.auemall.dao.TProductinfoPOMapper;
import com.auemall.po.ProductLadderPricePO;
import com.auemall.po.ProductLadderPricePOExample;
import com.auemall.po.ProductSkuPO;
import com.auemall.po.ProductSkuPOExample;
import com.auemall.po.TAttributeValuesPO;
import com.auemall.po.TAttributeValuesPOExample;
import com.auemall.po.TCategoryBrandsPO;
import com.auemall.po.TCategoryBrandsPOExample;
import com.auemall.po.TProductAttributePO;
import com.auemall.po.TProductAttributePOExample;
import com.auemall.po.TProductSkuItemsPO;
import com.auemall.po.TProductSkuItemsPOExample;
import com.auemall.po.TProductbrandPO;
import com.auemall.po.TProductbrandPOExample;
import com.auemall.po.TProductinfoPOExample;
import com.auemall.po.TProductinfoPOWithBLOBs;
import com.auemall.service.ItemBrandCheckedRespVO;
import com.auemall.service.ItemService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.utils.StringUtils;
import com.auemall.vo.AttributeValueVO;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.DeleteItemReqVO;
import com.auemall.vo.InsertItemSku;
import com.auemall.vo.ItemBrandVO;
import com.auemall.vo.ItemInfoVO;
import com.auemall.vo.ItemReqVO;
import com.auemall.vo.ItemSkuVO;
import com.auemall.vo.ProductLadderPriceVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



@Service
public class ItemServiceImpl implements ItemService {
    
    private Logger logger = Logger.getLogger(ItemServiceImpl.class);

	@Autowired
	private TProductinfoPOMapper productinfoPOMapper;     //产品信息表
	@Autowired
	private TProductbrandPOMapper productbrandPOMapper;   //品牌表
	@Autowired
	private TCategoryBrandsPOMapper tCategoryBrandsPOMapper;    //类别品牌关联表
	@Autowired
	private TProductAttributePOMapper productAttributePOMapper;   //产品属性表
	@Autowired
	private TAttributeValuesPOMapper tAttributeValuesPOMapper;   //产品属性值表
    @Autowired
    private ProductSkuPOMapper productSkuPOMapper;   //产品规格表
    @Autowired
    private TProductSkuItemsPOMapper tProductSkuItemsPOMapper;   //产品规格明细表
//    @Autowired
//    private TProductSkuPOMapper tProductSkuPOMapper;   //产品信息规格表
    
    @Autowired
    private ProductLadderPricePOMapper productLadderPricePOMapper;   //阶梯表
    @Autowired
    private TFavoritePOMapper tFavoritePOMapper;   //个人收藏信息表
    @Autowired
    private THomeProductPagePOMapper tHomeProductPagePOMapper;   //首页显示产品表
	

	/**
	 * 
	 * @Title: selectItemList 
	 * @Description: TODO(查询商品列表)
	 * @param page
	 * @param rows
	 * @returnR
	 * @author WangChao
	 */
    @Override
    public EUDataGridResult selectItemList(ItemReqVO req,int page, int rows) throws Exception{
        EUDataGridResult respVO = new EUDataGridResult();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        //查询商品列表
        TProductinfoPOExample example = new TProductinfoPOExample();
        TProductinfoPOExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(req.getBrandId())){
            criteria.andBrandIdEqualTo(Integer.parseInt(req.getBrandId()));
        }
        if(StringUtils.isNotBlank(req.getCategoryId())){
            criteria.andCategoryIdEqualTo(Integer.parseInt(req.getCategoryId()));
        }
        if(StringUtils.isNotBlank(req.getProductCode())){
            criteria.andProductCodeEqualTo(req.getProductCode());
        }
        if(null != req.getSaleStatus() && req.getSaleStatus().size() > 0){
            criteria.andSaleStatusIn(req.getSaleStatus());
        }
        if(StringUtils.isNotBlank(req.getProductName())){
            criteria.andProductNameLike("%"+req.getProductName()+"%");
        }
        if(StringUtils.isNotBlank(req.getAddTime_begin())){
            criteria.andAddTimeGreaterThanOrEqualTo(ApplicationConstant.dateTimeFormat.parse(req.getAddTime_begin()));
        }
        if(StringUtils.isNotBlank(req.getAddTime_end())){
            criteria.andAddTimeLessThanOrEqualTo(ApplicationConstant.dateTimeFormat.parse(req.getAddTime_end()));
        }
        PageHelper.startPage(page, rows);
        List<TProductinfoPOWithBLOBs> list = productinfoPOMapper.selectByExampleWithBLOBs(example);
        respVO.setRows(list);
        //取记录总条数
        PageInfo<TProductinfoPOWithBLOBs> pageInfo = new PageInfo<>(list);
        respVO.setTotal(pageInfo.getTotal());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询商品信息成功");
        return respVO;
    }

    /**
     * 
     * @Title: addItemInfo 
     * @Description: TODO(增加商品信息)
     * @param req
     * @return
     * @author WangChao
     */
    @Override
    @Transactional
    public BaseRespVO addItemInfo(ItemInfoVO req) {
        BaseRespVO respVO = new BaseRespVO();
        TProductinfoPOWithBLOBs record = new TProductinfoPOWithBLOBs();
        BeanUtils.copyProperties(req, record);
//      record.setSaleStatus((byte) 1);     // '销售状态:'  #0:回收站;1:出售中;2:下架区;3:仓库中
        record.setVistiTimes(0);            //访问次数,默认0
        record.setSaleCounts(0);            //销售数量,默认0
        record.setAddTime(new Date());
        record.setUpdateTime(new Date());
        productinfoPOMapper.insert(record);
        if(StringUtils.isNotBlank(req.getExtendattrs())){
            String extendattrs = req.getExtendattrs();
            String[] sourceStrArray = extendattrs.split(",");
            for(String valueid : sourceStrArray){
                TProductAttributePO record2 = new TProductAttributePO();
                record2.setValueId(Integer.parseInt(valueid));
                record2.setProductId(record.getId());
                productAttributePOMapper.insert(record2);
            }
        }
        if(StringUtils.isNotBlank(req.getSkuids())){
            String skuids = req.getSkuids();
            String[] skuidarray = skuids.split(",");
            for(String skuid : skuidarray){
//                TProductSkuPO record3 = new TProductSkuPO();
//                record3.setProductId(record.getId());
//                record3.setSkuId(Integer.parseInt(skuid));
//                tProductSkuPOMapper.insert(record3);
                ProductSkuPO record2 = new ProductSkuPO();
                record2.setId(skuid);
                record2.setProductId(record.getId());
                productSkuPOMapper.updateByPrimaryKeySelective(record2);
            }
        }
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("增加商品信息成功");
        return respVO;
    }

    /**
     * 
     * @Title: updateItemInfo 
     * @Description: TODO(修改商品信息)
     * @param req
     * @return
     * @author WangChao
     */
    @Override
    @Transactional
    public BaseRespVO updateItemInfo(ItemInfoVO req) {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(null == req.getId()){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[id]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TProductinfoPOWithBLOBs record = new TProductinfoPOWithBLOBs();
        BeanUtils.copyProperties(req, record);
        record.setUpdateTime(new Date());
        productinfoPOMapper.updateByPrimaryKeySelective(record);
        if(StringUtils.isNotBlank(req.getExtendattrs())){
            TProductAttributePOExample example = new TProductAttributePOExample();
            TProductAttributePOExample.Criteria criteria = example.createCriteria();
            criteria.andProductIdEqualTo(req.getId());
            productAttributePOMapper.deleteByExample(example);
            
            String extendattrs = req.getExtendattrs();
            String[] sourceStrArray = extendattrs.split(",");
            for(String valueid : sourceStrArray){
                TProductAttributePO record2 = new TProductAttributePO();
                record2.setValueId(Integer.parseInt(valueid));
                record2.setProductId(req.getId());
                productAttributePOMapper.insert(record2);
                
            }
        }
        /*if(StringUtils.isNotBlank(req.getSkuids())){
            TProductSkuPOExample example = new TProductSkuPOExample();
            TProductSkuPOExample.Criteria criteria = example.createCriteria();
            criteria.andProductIdEqualTo(req.getId());
            tProductSkuPOMapper.deleteByExample(example);
            
            String skuids = req.getSkuids();
            String[] skuidarray = skuids.split(",");
            for(String skuid : skuidarray){
                TProductSkuPO record3 = new TProductSkuPO();
                record3.setProductId(req.getId());
                record3.setSkuId(Integer.parseInt(skuid));
                tProductSkuPOMapper.insert(record3);
            }
        }*/
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改商品信息成功");
        return respVO;
    }

    /**
     * 
     * @Title: deleteItemInfos 
     * @Description: TODO(批量删除商品信息)
     * @param req
     * @return
     * @author WangChao
     * @throws Exception 
     */
    @Override
    @Transactional
    public BaseRespVO deleteItemInfos(DeleteItemReqVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(req.getIds() != null && req.getIds().size() > 0){
            for(Integer id : req.getIds()){
                //1、删除商品信息表
                productinfoPOMapper.deleteByPrimaryKey(id);
                //2、删除商品属性表
                TProductAttributePOExample example = new TProductAttributePOExample();
                TProductAttributePOExample.Criteria criteria = example.createCriteria();
                criteria.andProductIdEqualTo(id);
                productAttributePOMapper.deleteByExample(example);
                //3、删除对应该商品的规格表(调用删除规格的方法)
                ProductSkuPOExample example2 = new ProductSkuPOExample();
                ProductSkuPOExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andProductIdEqualTo(id);
                List<ProductSkuPO> list = productSkuPOMapper.selectByExample(example2);
                if(null != list && list.size() > 0){
                    for(ProductSkuPO po : list){
                        ItemSkuVO vo = new ItemSkuVO();
                        vo.setId(po.getId());
                        this.deleteItemSku(vo);
                    }
                }
            }
        }
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除商品信息以及商品对应的属性和规格成功");
        return respVO;
    }

    @Override
    public EUDataGridResult selectItemBrand(ItemBrandVO req, int page, int rows) throws Exception {
        EUDataGridResult respVO = new EUDataGridResult();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        //查询商品列表
        TProductbrandPOExample example = new TProductbrandPOExample();
        TProductbrandPOExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(req.getBrandImg())){
            criteria.andBrandImgEqualTo(req.getBrandImg());
        }
        if(StringUtils.isNotBlank(req.getBrandName())){
            criteria.andBrandNameLike("%"+req.getBrandName()+"%");
        }
        if(StringUtils.isNotBlank(req.getCompanyUrl())){
            criteria.andCompanyUrlEqualTo(req.getCompanyUrl());
        }
        if(StringUtils.isNotBlank(req.getId())){
            criteria.andIdEqualTo(Integer.parseInt(req.getId()));
        }
        if(StringUtils.isNotBlank(req.getLogo())){
            criteria.andLogoEqualTo(req.getLogo());
        }
        if(StringUtils.isNotBlank(req.getShowOnHomePage())){
            criteria.andShowOnHomePageEqualTo(Integer.parseInt(req.getShowOnHomePage()));
        }
        PageHelper.startPage(page, rows);
        List<TProductbrandPO> list = productbrandPOMapper.selectByExampleWithBLOBs(example);
        respVO.setRows(list);
        //取记录总条数
        PageInfo<TProductbrandPO> pageInfo = new PageInfo<>(list);
        respVO.setTotal(pageInfo.getTotal());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询商品品牌成功");
        return respVO;
    }

    @Override
    public ItemBrandCheckedRespVO selectItemBrandChecked(ItemBrandVO req) throws Exception {
        ItemBrandCheckedRespVO respVO = new ItemBrandCheckedRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getCategoryId())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[CategoryId]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        List<ItemBrandVO> respList = new ArrayList<ItemBrandVO>();
        List<TProductbrandPO> list = productbrandPOMapper.selectByExampleWithBLOBs(null);
        if(null != list && list.size() > 0){
            for(TProductbrandPO po : list){
                ItemBrandVO vo = new ItemBrandVO();
                BeanUtils.copyProperties(po, vo);
                vo.setId(po.getId().toString());
                TCategoryBrandsPOExample example = new TCategoryBrandsPOExample();
                TCategoryBrandsPOExample.Criteria criteria = example.createCriteria();
                criteria.andCategoryIdEqualTo(Integer.parseInt(req.getCategoryId()));
                List<TCategoryBrandsPO> selectByExample = tCategoryBrandsPOMapper.selectByExample(example);
                if(null != selectByExample && selectByExample.size() > 0){
                    for(TCategoryBrandsPO categoryBrandsPO : selectByExample){
                        if(categoryBrandsPO.getBrandId().equals(po.getId())){
                            vo.setChecked("1");
                        }
                    }
                    if(StringUtils.isBlank(vo.getChecked())){
                        vo.setChecked("0");
                    }
                }
                respList.add(vo);
            }
        }
        respVO.setList(respList);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询商品品牌[包括选定品牌]成功");
        return respVO;
    }

    @Override
    public BaseRespVO addItemBrand(ItemBrandVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TProductbrandPO record = new TProductbrandPO();
        BeanUtils.copyProperties(req, record);
        if(StringUtils.isNotBlank(req.getShowOnHomePage())){
            record.setShowOnHomePage(Integer.parseInt(req.getShowOnHomePage()));
        }
        productbrandPOMapper.insertSelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("增加商品品牌成功");
        return respVO;
    }

    @Override
    public BaseRespVO updateItemBrand(ItemBrandVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getId())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TProductbrandPO record = new TProductbrandPO();
        BeanUtils.copyProperties(req, record);
        record.setId(Integer.parseInt(req.getId()));
        if(StringUtils.isNotBlank(req.getShowOnHomePage())){
            record.setShowOnHomePage(Integer.parseInt(req.getShowOnHomePage()));
        }
        productbrandPOMapper.updateByPrimaryKeySelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改商品品牌成功");
        return respVO;
    }

    @Override
    public BaseRespVO deleteItemBrand(ItemBrandVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getId())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        productbrandPOMapper.deleteByPrimaryKey(Integer.parseInt(req.getId()));
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除商品品牌成功");
        return respVO;
    }

    @Override
    @Transactional
    public InsertItemSku insertItemSku(ItemSkuVO req) {
        InsertItemSku respVO = new InsertItemSku();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(null == req.getAttributeIds() || req.getAttributeIds().size() == 0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请选择规格属性！");
            logger.error(respVO.getMsg());
            return respVO;
        }
       /* if(StringUtils.isNotBlank(req.getProductinfoId())){
            ProductSkuPOExample example = new ProductSkuPOExample();
            ProductSkuPOExample.Criteria criteria = example.createCriteria();
            criteria.andSkuNameEqualTo(req.getSkuName());
            List<ProductSkuPO> selectByExample = productSkuPOMapper.selectByExample(example);
            if(selectByExample != null && selectByExample.size() > 0){
                TProductSkuPOExample example2 = new TProductSkuPOExample();
                TProductSkuPOExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andProductIdEqualTo(Integer.parseInt(req.getProductinfoId()));
                criteria2.andSkuIdEqualTo(selectByExample.get(0).getId());
                int countByExample = tProductSkuPOMapper.countByExample(example2);
                if(countByExample >0){
                    respVO.setResult(ApplicationConstant.RESULT_FALTURE);
                    respVO.setMsg("已添加该规格");
                    logger.error(respVO.getMsg());
                    return respVO;
                }
            }
        }else{
            ProductSkuPOExample example = new ProductSkuPOExample();
            ProductSkuPOExample.Criteria criteria = example.createCriteria();
            criteria.andSkuNameEqualTo(req.getSkuName());
            int countByExample = productSkuPOMapper.countByExample(example);
            if(countByExample >0){
                respVO.setResult(ApplicationConstant.RESULT_FALTURE);
                respVO.setMsg("已添加该规格");
                logger.error(respVO.getMsg());
                return respVO;
            }
        }*/
        ProductSkuPO record = new ProductSkuPO();
//        BeanUtils.copyProperties(req, record);
        record.setId(UUID.randomUUID().toString());
        if(StringUtils.isNotBlank(req.getProductinfoId())){
            record.setProductId(Integer.parseInt(req.getProductinfoId())); //修改时直接插
        }else{
            record.setProductId(0);     //设置默认 关联产品id  0  (新增时)
        }
        record.setCostPrice(0.0);  //设置默认价格0.0
        productSkuPOMapper.insertSelective(record);
        for(Integer attrbuteId : req.getAttributeIds()){
            TProductSkuItemsPO record3 = new TProductSkuItemsPO();
            record3.setAttributeValueId(attrbuteId);
            record3.setSkuId(record.getId());
            tProductSkuItemsPOMapper.insert(record3);
        }
        if(req.getStepprices() != null){
            for(String stepprice : req.getStepprices()){
                String [] steppricearray = stepprice.split(",");
                String [] steppricearraybeginend = steppricearray[0].split("_");
                ProductLadderPricePO productLadderPriceRecord = new ProductLadderPricePO();
                productLadderPriceRecord.setMinCount(Integer.parseInt(steppricearraybeginend[0]));
                productLadderPriceRecord.setMaxCount(Integer.parseInt(steppricearraybeginend[1]));
                productLadderPriceRecord.setPrice(Double.parseDouble(steppricearray[1]));
                productLadderPriceRecord.setSkuId(record.getId().toString());
                productLadderPricePOMapper.insert(productLadderPriceRecord);
//                TAttributeValuesPO record2 = new TAttributeValuesPO();
//                record2.setAttributeId(Integer.parseInt(steppricearray[0]));
//                record2.setValueStr(steppricearray[1]);
//                tAttributeValuesPOMapper.insert(record2);
            }
        }
        //商品id不为空同时插入关联表
//        if(StringUtils.isNotBlank(req.getProductinfoId())){
//            TProductSkuPO record4 = new TProductSkuPO();
//            record4.setProductId(Integer.parseInt(req.getProductinfoId()));
//            record4.setSkuId(record.getId());
//            tProductSkuPOMapper.insert(record4);
//        }
        respVO.setSkuId(record.getId());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("新增商品规格成功");
        return respVO;
    }

    @Override
    public EUDataGridResult selectItemSku(ItemSkuVO req) {
        EUDataGridResult respVO = new EUDataGridResult();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
       /* List<Integer> notInSkus = new ArrayList<Integer>();
        List<TProductSkuPO> productskus;
        //产品id不为空，修改时调用，查询对应该产品的规格列表(这里ProductinfoId是商品表主键，不是货号)
        if(StringUtils.isNotBlank(req.getProductinfoId())){
            TProductSkuPOExample example2 = new TProductSkuPOExample();
            TProductSkuPOExample.Criteria criteria = example2.createCriteria();
            criteria.andProductIdEqualTo(Integer.parseInt(req.getProductinfoId()));
             productskus = tProductSkuPOMapper.selectByExample(example2);
        //产品id为空，新增时调用，（查未与商品关联的规格，即未发布的）
        }else{
             productskus = tProductSkuPOMapper.selectByExample(null);
        }
        if(productskus.size() == 0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("该商品还没有添加规格");
            return respVO;
        }
        if(productskus != null && productskus.size() >0){
            for(TProductSkuPO po : productskus){
                notInSkus.add(po.getSkuId());
            }
        }*/
        List<ItemSkuVO> respList = new ArrayList<ItemSkuVO>();
        ProductSkuPOExample example = new ProductSkuPOExample();
        ProductSkuPOExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(req.getProductinfoId())){
            criteria.andProductIdEqualTo(Integer.parseInt(req.getProductinfoId()));
        }else{
            criteria.andProductIdEqualTo(0);  //默认是  0
        }
       /* if(notInSkus != null && notInSkus.size() > 0){
            if(StringUtils.isNotBlank(req.getProductinfoId())){
                criteria.andIdIn(notInSkus);
            }else{
                criteria.andIdNotIn(notInSkus);
            }
        }*/
        //1、查询规格表的规格信息
        List<ProductSkuPO> list = productSkuPOMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            for(ProductSkuPO po : list){
                ItemSkuVO vo = new ItemSkuVO();
                BeanUtils.copyProperties(po, vo);
                //2、查询阶梯价格表，获取对应该规格的阶梯价格
                List<ProductLadderPriceVO> productLadderPrices = new ArrayList<ProductLadderPriceVO>();
                ProductLadderPricePOExample example4 = new ProductLadderPricePOExample();
                ProductLadderPricePOExample.Criteria criteria4 = example4.createCriteria();
                criteria4.andSkuIdEqualTo(po.getId());
                List<ProductLadderPricePO> productLadderPriceList = productLadderPricePOMapper.selectByExample(example4);
                if(null != productLadderPriceList && productLadderPriceList.size() > 0){
                    for(ProductLadderPricePO ladderPricePO : productLadderPriceList){
                        ProductLadderPriceVO ladderPriceVO = new ProductLadderPriceVO();
                        BeanUtils.copyProperties(ladderPricePO, ladderPriceVO);
                        productLadderPrices.add(ladderPriceVO);
                    }
                    vo.setProductLadderPrices(productLadderPrices);
                }
                //3、查询规格属性值关联表，获取规格名称（规格名称由属性值拼接而成）
                List<AttributeValueVO> attrValues = new ArrayList<AttributeValueVO>();
                TProductSkuItemsPOExample example2 = new TProductSkuItemsPOExample();
                TProductSkuItemsPOExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andSkuIdEqualTo(po.getId());
                List<TProductSkuItemsPO> selectByExample = tProductSkuItemsPOMapper.selectByExample(example2);
                if(selectByExample != null && selectByExample.size() > 0){
                    for(TProductSkuItemsPO itemsPO : selectByExample){
                        TAttributeValuesPOExample example3 = new TAttributeValuesPOExample();
                        TAttributeValuesPOExample.Criteria criteria3 = example3.createCriteria();
                        criteria3.andIdEqualTo(itemsPO.getAttributeValueId());
                        List<TAttributeValuesPO> selectByExample2 = tAttributeValuesPOMapper.selectByExample(example3);
                        if(selectByExample2 != null && selectByExample2.size() > 0){
                            for(TAttributeValuesPO attributeValuesPO : selectByExample2){
                                AttributeValueVO attributeValueVO = new AttributeValueVO();
                                BeanUtils.copyProperties(attributeValuesPO, attributeValueVO);
                                attributeValueVO.setId(attributeValuesPO.getId().toString());
                                if(null != attributeValuesPO.getAttributeId()){
                                    attributeValueVO.setAttributeId(attributeValuesPO.getAttributeId().toString());
                                }
                                attrValues.add(attributeValueVO);
                            }
                        }
                    }
                    vo.setAttrValues(attrValues);
                }
                respList.add(vo);
            }
        }
        respVO.setRows(respList);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询商品规格成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO updateItemSku(ItemSkuVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getId())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        ProductSkuPO record = new ProductSkuPO();
        BeanUtils.copyProperties(req, record);
        productSkuPOMapper.updateByPrimaryKeySelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改商品规格成功");
        return respVO;
        
    }

    @Override
    @Transactional
    public BaseRespVO deleteItemSku(ItemSkuVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getId())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        //1删除规格表
        productSkuPOMapper.deleteByPrimaryKey(req.getId());
        //2删除阶梯价格表
        ProductLadderPricePOExample example3 = new ProductLadderPricePOExample();
        ProductLadderPricePOExample.Criteria criteria3 = example3.createCriteria();
        criteria3.andSkuIdEqualTo(req.getId());
        productLadderPricePOMapper.deleteByExample(example3);
        //删除属性规格关联表
        TProductSkuItemsPOExample example = new TProductSkuItemsPOExample();
        TProductSkuItemsPOExample.Criteria criteria = example.createCriteria();
        criteria.andSkuIdEqualTo(req.getId());
//        List<TProductSkuItemsPO> selectByExample = tProductSkuItemsPOMapper.selectByExample(example);
//        if(selectByExample != null && selectByExample.size() > 0){
//            for(TProductSkuItemsPO po : selectByExample){
//                tAttributeValuesPOMapper.deleteByPrimaryKey(po.getAttributeValueId());
//            }
//        }
        tProductSkuItemsPOMapper.deleteByExample(example);
        //4商品id不为空同时删除关联表（修改商品信息时）
//        if(StringUtils.isNotBlank(req.getProductinfoId())){
//            TProductSkuPOExample example2 = new TProductSkuPOExample();
//            TProductSkuPOExample.Criteria criteria2 = example2.createCriteria();
//            criteria2.andProductIdEqualTo(Integer.parseInt(req.getProductinfoId()));
//            criteria2.andSkuIdEqualTo(req.getId());
//            tProductSkuPOMapper.deleteByExample(example2);
//        }
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除商品规格以及对应属性成功");
        return respVO;
    }


}
